﻿//using DG.Tweening;
using LuaInterface;
using System;
using UnityEngine;
using FairyGUI;

public static class TweenUtils
{
	public static GTweener TweenFloat(float start, float end, float duration, LuaFunction OnUpdate)
	{
        return GTween.To(start, end, duration).OnUpdate((GTweener tweener) =>
        {
            try
            {
                OnUpdate.Call(tweener.value.x);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }).OnComplete(() =>
        {
            OnUpdate.Dispose();
            OnUpdate = null;
        });
    }

    public static GTweener TweenVector2(Vector2 start, Vector2 end, float duration, LuaFunction OnUpdate)
	{
        return GTween.To(start, end, duration).OnUpdate((GTweener tweener) =>
        {
            try
            {
                OnUpdate.Call(tweener.value.vec2);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }).OnComplete(() =>
        {
            OnUpdate.Dispose();
            OnUpdate = null;
        });
	}

	public static GTweener TweenVector3(Vector3 start, Vector3 end, float duration, LuaFunction OnUpdate)
	{
        return GTween.To(start, end, duration).OnUpdate((GTweener tweener) =>
        {
            try
            {
                OnUpdate.Call(tweener.value.vec3);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }).OnComplete(() =>
        {
            OnUpdate.Dispose();
            OnUpdate = null;
        });
        //return DOTween.To(() => start, x => OnUpdate.Call(x), end, duration).OnComplete(() =>
        //{
        //	OnUpdate.Dispose();
        //	OnUpdate = null;
        //});
    }

	public static void SetEaseByInt(GTweener tweener, int easeType)
	{
		tweener.SetEase((EaseType)easeType);
	}

	public static void OnComplete(GTweener tweener, LuaFunction func)
	{
		tweener.OnComplete(() =>
		{
			try
			{
				func.Call();
			}
			catch (Exception e)
			{
				Debug.LogError(e);
			}
			func.Dispose();
			func = null;
		});
	}

	public static void OnComplete(GTweener tweener, LuaFunction func, object self)
	{
		tweener.OnComplete(() =>
		{
			try
			{
				func.Call(self);
			}
			catch (Exception e)
			{
				Debug.LogError(e);
			}
			func.Dispose();
			func = null;
			if (self is LuaTable)
			{
				((LuaTable)self).Dispose();
				self = null;
			}
		});
	}

	public static void SetDelay(GTweener tweener, float delay)
	{
		tweener.SetDelay(delay);
	}

	public static void SetLoops(GTweener tweener, int loops)
	{
		tweener.SetRepeat(loops);
	}

	public static void SetLoops(GTweener tweener, int loops, bool yoyo)
	{
		tweener.SetRepeat(loops, yoyo);
	}

	public static void SetTarget(GTweener tweener, object target)
	{
		tweener.SetTarget(target);
	}

    public static void KillTween(GTweener tweener, bool complete)
    {
        tweener.Kill(complete);
    }
}
